$(function(){
	goodsDetailApp.refresh();
})
var goodsDetailApp = function(){
	return{
		AddCollect:function(e){
			var url = $("#APP_ROOT_PATH").val() + 'collect/create';
			var goodsId = $("#goodsId").val();
			var arg={goodsId:goodsId};
			var flag=$(e).find(".collect").is(".bg-o");
			if(flag){
				url=$("#APP_ROOT_PATH").val() + 'collect/deleteByMemberId';
				arg={id:$("#COLLECT").val()};
			}
			mui.util.Ajax.asyncGET(url,arg,function(res){
			
					if (flag) {
						if(res==true){
							layer.open({
						    content: '删除收藏成功',
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
						 });
						$(e).find(".collect").removeClass("bg-o");
						}else{
							layer.open({
						    content: res.message,
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
						 });
							}
					} else{
							if(res>0){
									layer.open({
								    content: '收藏成功',
								    skin: 'msg',
								    time: 2 //2秒后自动关闭
								 });
								$(e).find(".collect").addClass("bg-o");
								$("#COLLECT").val(res);
								}else{
										layer.open({
								    content: res.message,
								    skin: 'msg',
								    time: 2 //2秒后自动关闭
								 	});
							}
					}
					
				
			});
		},
		/**
		 * 刷新购物车数量
		 */
		refresh:function(){
			var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/count';
			mui.util.Ajax.asyncGET(url,{},function(res){
					$("#shopping_chart_num").html(res);
			});
		
		},
		//加入购物车
		AddCar:function(e){
			var url = $("#APP_ROOT_PATH").val() + 'shopping_chart/join';
			var arg={
					goodsId:$("#goodsId").val(),
					num:1
					};
				mui.util.Ajax.asyncGET(url,arg,function(res){
						if(res>0){
						goodsDetailApp.refresh();
						layer.open({
						    content: '加入购物车成功',
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
						 });
						}else{
							layer.open({
						    content: res.message,
						    skin: 'msg',
						    time: 2 //2秒后自动关闭
						 });
						}
			});
		},
	}
}();